<?php
namespace app\admin\controller;
use app\model\WxGoodsCategory as WxGoodsCategoryModel;
use think\Db;


class WxGoodsCategory extends Base
{
    public function __construct(){
        $model = new WxGoodsCategoryModel();
        $this->model = $model;
        parent::__construct();
        
        $page_info = $this->page_info;
        $page_info['title'] = '案例分类';

        $this->assign([
            'model'=>$this->model,
            'page_info'=>$page_info
        ]);
    }

    public function assignFields(){
        $model = $this->model;

        $fields = [
            'name'=>['name'=>'名称','type'=>'text','valids'=>['required']],
            'sort'=>['name'=>'排序值','type'=>'text'],
            'location'=>['name'=>'显示位置','type'=>'checkbox','params'=>$model->getLocationList()],
            'image'=>['name'=>'封面','type'=>'file'],
            
        ];
        $valids = createPageValid($fields);
        $this->assign([
            'fields'=>$fields,
            'valids'=>$valids
        ]);

    }

    public function assignSearchs(){
        $model = $this->model;
        $search_params = [
            [
                ['key'=>'name','name'=>'名称','type'=>'text'],
            ]
        ];
        $this->assign([
            'search_params'=>$search_params
        ]);
    }

    public function index(){
        $model = $this->model;
        $map_params = [
            ['key'=>'name','type'=>'like'],
        ];
        $map = getSearchParam($map_params);

        $page_param = [
            'page'=>input('page',1),
            'list_rows'=>input('page_size',20)
        ];
        // 显示筛选
        $this->assignSearchs();

        $list = $model->where($map)->order('id','desc')->paginate($page_param);
        $page = $list->render();
        $list = $list->toarray();
        foreach ($list['data'] as $k => $vo) {
            if (!empty($vo['location'])) {
                $vo['location'] = explode(',',$vo['location']);
                $locations = [];
                foreach ($vo['location'] as $location) {
                    $locations[] = $model->getLocationCn($location);
                }
                $list['data'][$k]['location'] = implode(',',$locations);
            }
        }
        return view('index', [
            'list'=>$list,
            'page'=>$page
        ]);
    }

    public function add(){
        if (request()->isPost()) {
            $fields = [
                'must'=>['name'],
                'nomust'=>['sort','location','image']
            ];
            $save_data = paramFilter(request()->param(),$fields);
            if (!empty($save_data['error_msg'])) {
                $this->error($save_data['error_msg']);
            }
            if (!empty($save_data['location'])) {
                $save_data['location'] = implode(',',$save_data['location']);
            }
        }

        return parent::saveData($save_data);
    }

    public function edit(){
        $id = input('id');
        if (empty($id)) { $this->error('id不能为空'); }
        
        $model = $this->model;
        if (request()->isPost()) {
            $fields = [
                'must'=>['name'],
                'nomust'=>['sort','location','image']
            ];
            $save_data = paramFilter(request()->param(),$fields);
            if (!empty($save_data['error_msg'])) {
                $this->error($save_data['error_msg']);
            }
            if (!empty($save_data['location'])) {
                $save_data['location'] = implode(',',$save_data['location']);
            }
        }

        return parent::updateData($id,$save_data);
    }




   
}
